//服务器地址
import {server} from '../config/index.js';

castapp.init();
mui.init();


var avatarEle = document.querySelector('#avatar');
var saveEle = document.querySelector('#save');
var liElems = document.querySelectorAll('.mui-table-view-cell');
var sexElem = liElems[1].querySelector('span.sex');
var ageElem = liElems[2].querySelector('input.age');

var userInfo = localStorage.getItem('userInfo');
userInfo = userInfo? JSON.parse(userInfo): {};

var dataURL;

initInfo();
function initInfo(){
    var avatar = userInfo.avatar;
    var nickname = userInfo.nickname;
    var BB_num = userInfo.BB_num;
    if(avatar){
        avatarEle.src = avatar;
    }
    castapp.ajax({
        url: server+ '/api/user/get_person_info',
        type: 'get',
        data: {
            nickname: nickname,
            BB_num : BB_num
        },
        succFn: function(res){
            castapp.closeWaiting();
            res = JSON.parse(res);
            if(res.msg=='OK'){
                sexElem.innerHTML = res.data.sex;
                ageElem.value = res.data.age;
                if(!avatar){
                    avatarEle.src = res.data.avatar;
                }
                castapp.getCurrentInterface(function(obj){
                    obj.show();
                });
            }
        }
    });
};


//点击更换头像
liElems[0].addEventListener('tap',function(e){
    e.preventDefault();
    castapp.actionSheet(['照相机','相册'],{
        succFn: function(data){
            //图片压缩
            mui.plusReady(function(){
                var bitmap = new plus.nativeObj.Bitmap();
                bitmap.load(data, function(){
                    bitmap.save('_doc/imgs/avatar.jpg',{
                        overwrite: true,
                        format: 'jpg',
                        quality: 10
                    }, function(){
                        dataURL = bitmap.toBase64Data();
                        //压缩成功
                        var dataPath = plus.io.convertLocalFileSystemURL('_doc/imgs/avatar.jpg');
                        userInfo.avatar = dataPath;
                        avatarEle.src = dataPath;
                        localStorage.setItem('userInfo', JSON.stringify(userInfo));
                        
                    }, function(e){
                        castapp.prompt('保存图片失败，请重新选择');
                    });
                },function(){
                    castapp.prompt('加载图片失败，请重新选择');
                });
            })
        },
        errFn: function(err){

        }
    });
});

//点击更改性别
liElems[1].addEventListener('tap',function(e){
    e.preventDefault();
    mui.plusReady(function(){
        // 弹出系统选择按钮框
        var buttonArr = [{title:"男"},{title:"女"}];
        plus.nativeUI.actionSheet({
            buttons: buttonArr
        }, function(e){
            if(e.index ==1|| e.index==2){
                var choose_sex = buttonArr[e.index-1].title;
                sexElem.innerHTML = choose_sex;
            }
        } );
    });
});

//输入内容
ageElem.oninput = function(e){
    e.preventDefault();
    var input_val = Number(this.value.trim());
    if(!input_val){
        castapp.prompt('请输入正确年龄');
        return;
    }
}


//点击save按钮
saveEle.addEventListener('tap', function(e){
    e.preventDefault();
    var sex = sexElem.innerHTML;
    var age = Number(ageElem.value.trim());
    if(!age){
        castapp.prompt('请输入正确年龄');
        return;
    }
    castapp.ajax({
        url: server+'/api/user/modify_info',
        type: 'post',
        data: {
            nickname: userInfo.nickname,
            BB_num: userInfo.BB_num,
            avatar: dataURL,
            sex: sex,
            age: age
        },
        succFn: function(res){
            console.log(res);
            res = JSON.parse(res);
            if(res.msg =='OK'){
                castapp.prompt('保存成功');
            }else{

            }
        },
        errFn: function(err){
            castapp.prompt('服务错误，请重新选择');
        }
    });
});
